﻿using MyOCR_WPF.Tool;
using Newtonsoft.Json;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace MyOCR_WPF.OCRHelper
{
    public class YouDaoOCR : IOCR
    {
        // 有道云中开通对应服务应用的 API Key 建议开通应用的时候多选服务
        private static String SecretId;
        // 有道云中开通对应服务应用的 Secret Key
        private static String SecretKey;

        static TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
        Int64 currentTime = Convert.ToInt64(ts.TotalSeconds);
        public YouDaoOCR()
        {
            SecretId = ConfigurationManager.AppSettings["YouDao_Id"];
            SecretKey = ConfigurationManager.AppSettings["YouDao_Key"];
        }

        public string SendImg(Bitmap img)
        {
            string authHost = "https://openapi.youdao.com/ocrapi";
            RestClient restClient = new RestClient(authHost);
            RestRequest request = new RestRequest(Method.POST);
            
            string baseImg = ImageProcessing.ImageToBase64(img);

            string signStr = SecretId + Truncate(baseImg) + "011225" + currentTime + SecretKey;

            request.AddParameter("img", baseImg);
            request.AddParameter("langType", "auto");
            request.AddParameter("detectType", "10012");
            request.AddParameter("imageType", "1");
            request.AddParameter("appKey", "7808551f7259284a");
            request.AddParameter("curtime", currentTime);
            request.AddParameter("salt", "011225");
            request.AddParameter("sign", ComputeHash(signStr, new SHA256CryptoServiceProvider()));
            request.AddParameter("docType", "json");
            request.AddParameter("signType", "v3");

            var result = restClient.Execute(request);
            dynamic json = JsonConvert.DeserializeObject(result.Content);
            var lst = new List<string>();
            foreach (var item in json.Result.regions)
            {
                foreach (var line in item.lines)
                {
                    var a = line.text;
                    lst.Add(line.text.ToString());
                }
            }
            return string.Join("", lst);
        }

        //下面两个都是官方实例http://ai.youdao.com/DOCSIRMA/html/%E6%96%87%E5%AD%97%E8%AF%86%E5%88%ABOCR/API%E6%96%87%E6%A1%A3/%E9%80%9A%E7%94%A8%E6%96%87%E5%AD%97%E8%AF%86%E5%88%AB%E6%9C%8D%E5%8A%A1/%E9%80%9A%E7%94%A8%E6%96%87%E5%AD%97%E8%AF%86%E5%88%AB%E6%9C%8D%E5%8A%A1-API%E6%96%87%E6%A1%A3.html
        protected static string Truncate(string q)
        {
            if (q == null)
            {
                return null;
            }
            int len = q.Length;
            return len <= 20 ? q : (q.Substring(0, 10) + len + q.Substring(len - 10, 10));
        }
        protected static string ComputeHash(string input, HashAlgorithm algorithm)
        {
            Byte[] inputBytes = Encoding.UTF8.GetBytes(input);
            Byte[] hashedBytes = algorithm.ComputeHash(inputBytes);
            return BitConverter.ToString(hashedBytes).Replace("-", "");
        }
    }
}
